//
//  LGTeamManagerController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/11.
//  团队列表

#import "LGTeamManagerController.h"
//申请记录
#import "LGTeamApplyRecordController.h"
//团队详情
#import "LGTeamDetailViewController.h"
#import "LGTeamManagerCell.h"
@interface LGTeamManagerController ()<UITableViewDelegate,UITableViewDataSource,LGBottomButtonDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *recordBtn;
/**   */
@property (nonatomic , strong)UIButton *joinBtn;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGTeamManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = (self.type == LGTeamManagerTypeMine)?@"我的团队":@"加入团队";
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor colorWithString:@"#F8F8F8"];
    self.emptyView.backgroundColor = [UIColor whiteColor];
    self.emptyView.cornerRidus = 10;
    [self emptyViewWithSuperView:self.view frame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(255))];
    if (self.type == LGTeamManagerTypeMine) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.recordBtn];
        self.joinBtn = [UIButton buttonWithBackColor:kThemeColor cornerRadius:viewPix(18) title:@"加入团队" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" frame:CGRectMake((Screen_W-viewPix(32)-viewPix(126))/2.0, viewPix(180), viewPix(126), viewPix(36))];
        [self.joinBtn addTarget:self action:@selector(bottomButtonTouched) forControlEvents:UIControlEventTouchUpInside];
        [self.emptyView addSubview:self.joinBtn];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    NSString *action = (self.type == LGTeamManagerTypeMine)?@"/team/myTeam":@"/team/canJoin";
    [RequestUtil withGET:action parameters:@{@"doctorId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无团队" offset:-50];
            if (self.type == LGTeamManagerTypeMine) {
                [self requestApplyData];
            }
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无团队" offset:-50];
        }
    }];
}

-(void)requestApplyData{
    [RequestUtil withGET:@"/team/myApprove" parameters:@{@"doctorId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSArray *tempArray = returnData;
            if (tempArray.count>0) {
                NSDictionary *tempDic = tempArray[0];
                if ([tempDic[@"status"] integerValue] == 0) {
                    [self.emptyView showViewWithImage:@"emptyView2" content:[NSString stringWithFormat:@"您已申请加入%@,请等待审核",tempDic[@"teamName"]] offset:-50];
                    self.joinBtn.hidden = YES;
                }else{
                    [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无团队" offset:-50];
                    self.joinBtn.hidden = NO;
                }
            }else{
                [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无团队" offset:-50];
                self.joinBtn.hidden = NO;
            }
        }
    } error:^(NSError *error) {
        
    }];
}

//申请记录
-(void)checkApplyRecordAction{
    LGTeamApplyRecordController *controller = [[LGTeamApplyRecordController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

//加入团队
-(void)bottomButtonTouched{
    LGTeamManagerController *controller = [[LGTeamManagerController alloc]init];
    controller.type = LGTeamManagerTypeJoin;
    controller.backVC = self;
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGTeamManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.tipButton.hidden = (self.type == LGTeamManagerTypeMine)?NO:YES;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGTeamDetailViewController *controller = [[LGTeamDetailViewController alloc]init];
    controller.teamid = LGNSString(tempDic[@"id"]);
    if (self.type == LGTeamManagerTypeJoin) {
        controller.actionTitle = @"申请加入";
        controller.backVC = self.backVC;
    }else{
        controller.actionTitle = [tempDic[@"manager"] integerValue]>0?@"解散团队":@"退出团队";
    }
    [self.navigationController pushViewController:controller animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGTeamManagerCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(8))];
    }
    return _tableView;
}

-(UIButton *)recordBtn{
    if(!_recordBtn){
        _recordBtn = [UIButton buttonWithTitle:@"申请记录" titleFont:LGFont(14) textColor:kThemeColor frame:CGRectMake(0, 0, viewPix(60), 44)];
        [_recordBtn addTarget:self action:@selector(checkApplyRecordAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _recordBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
